/**
 * koa-mvc-cli/webpack-config
 * @author HuiSir q273250950
 * @git https://gitee.com/huisir001/
 * @blog http://www.zuifengyun.com/
 */

/**
 * package.json脚本配置 
"scripts": {
    "dev": "node bin/www --dev",
    "start": "node bin/www",
    "wp-dev": "webpack --dev",
    "wp-pro": "webpack"
} 
*/

const path = require('path');
const Webpack = require('webpack');
const CssExtractPlugin = require('mini-css-extract-plugin');	//css分离
const CopyWebpackPlugin = require("copy-webpack-plugin");		//拷贝文件
const OptimizeCssPlugin = require('optimize-css-assets-webpack-plugin'); //CSS压缩
const isDev = process.argv.includes('--dev');   //从命令行判断是否为开发环境

/* 输出配置文件 */	
let config = {

    mode:isDev?'development':'production',

	/* 入口文件 */
	entry: {
		base:path.resolve(__dirname, "src/base.js")
    },
    
    /* 出口文件 */
	output: {
		filename: 'js/[name].js',
		chunkFilename:'js/[name].js',
		path: path.resolve(__dirname, isDev?'dev_static':'static'),
		//publicPath:'',
    },
    
    /* 实时打包 */
	watch:isDev?true:false,	
	watchOptions:isDev?{
		poll:1000,	//每秒询问1000次
		aggregateTimeout:500,	//超时500毫秒
		ignored:/node_modules/	//忽略module文件夹
    }:{},
    
	/* 模块和loader */
	module:{
		//loader
		rules:[
			
			/* 字体文件loader */
			{
				test: /\.(woff|woff2|svg|eot|ttf)$/i,
				use: 'url-loader?prefix=font&name=fonts/[name].[ext]'
			},
			
			/* 图片 Loader 放到规则的最前面（自下而上的执行顺序） */
			{
			    test: /\.(jpg|jpeg|svg|png|gif)$/i,	//i忽略大小写
				include:path.resolve(__dirname,'src/img'),	//限定只处理img目录
				use:"url-loader?limit=3072&name=img/[name].[ext]",	//使用query方式简写配置项
			},
			
			/* JS Loader ES6解析*/
			{
				test: /(\.jsx|\.js)$/,
                use: {
					loader:'babel-loader',
					options:{
						presets:[
							'@babel/preset-env'
						],
						plugins: [	//处理一些提案中的高级语法（不知怎么回事，不可用）
							["@babel/plugin-transform-runtime",{"corejs": 3}], //转换诸如promise,set,map,includes语法
						],
					}
				},	 
				include:path.resolve(__dirname,'src'),	//限定只处理src目录
			},
			
			/* CSS Loader */ 
			{
				test:/\.css$/,
				use: [
					{
						loader: CssExtractPlugin.loader,
						options: {
							publicPath: '../',	//避免图片路径出错
						}
					},
	                { 
	                	loader: 'css-loader', 
	                },
	                {
	                	loader:"postcss-loader",
	                	options: {
	                        plugins: [
						        require('autoprefixer')
	                        ]
                    	}
	                }
                ]
			},
			
			/* SASS Loader */
			{
                test: /(\.scss|\.sass)$/,
				include:path.resolve(__dirname,'src/css'),
                use: [
	                {
	                	loader: CssExtractPlugin.loader,
	                	options: {
							publicPath: '../'
	                	}
	                },
	                'css-loader',
	                {
	                	loader:"postcss-loader",
	                	options: {	
	                        plugins: [
						        require('autoprefixer')
	                        ]
                    	}
	                },
	                "sass-loader"
                ]
            },
		]
	},
	
	plugins:[
		/*css文件分离*/
        new CssExtractPlugin({
	      	filename: "css/[name].css",
	      	chunkFilename: "css/[name].css" 	//压缩后的文件名
    	}),
		
		/* 拷贝文件 */
 		new CopyWebpackPlugin([
			{from:'src/plugin',to:'./js'}	//将plugin目录文件拷到打包文件夹/js下
        ]),
         
		/* 版权信息 */
		new Webpack.BannerPlugin('@author HuiSir(q273250950) 版权所有,翻版必究'),
	],		
};

if(isDev){
    config.devtool='source-map';    //开启地图
}else{
    config.plugins.push(new OptimizeCssPlugin());   //css压缩
}

// 导出
module.exports = config